package com.leyou.common.advice;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author 虎哥
 */
@Slf4j
@Aspect
@Component
public class CommonLogAdvice {

    @Around("within(@org.springframework.stereotype.Service *)")
    public Object handleExceptionLog(ProceedingJoinPoint jp) throws Throwable {
        try {
            // 记录方法入参
            log.debug("{}方法准备调用，参数: {}", jp.getSignature(), Arrays.toString(jp.getArgs()));
            // 计时器
            long a = System.currentTimeMillis();
            // 调用被拦截的方法
            Object result = jp.proceed();
            // 记录方法处理结果
            log.debug("{}方法调用成功，执行耗时{}", jp.getSignature(), System.currentTimeMillis() - a);
            // 返回结果
            return result;
        } catch (Throwable throwable) {
            log.error(throwable.getLocalizedMessage(), throwable);
            throw throwable;
        }
    }
}